<template>
  <div>
    <el-row type="flex" justify="end">
      <!-- 年份 -->
      <el-select v-model="currentYear" class="electYear" size="small" @change="getcurrentDate">
        <el-option
          v-for="year in yearList"
          :key="year"
          :value="year"
          :label="year + '年'"
        />
      </el-select>

      <!-- 月份 -->
      <el-select v-model="currentMonth" class="electMonth" size="small" @change="getcurrentDate">
        <el-option
          v-for="month in 12"
          :key="month"
          :value="month"
          :label="month + '月'"
        />
      </el-select>
    </el-row>

    <!-- 日历 -->
    <el-calendar v-model="currentDate">
      <!-- date  data  是插件定义的 是作用域插槽渲染当前日期-->
      <template #dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <!-- 周末休 -->
          <span v-if="isWeek(date, data)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(oldval) {
      // 把- 格式切掉
      const day = oldval.split('-')[2]
      // 有0开头就切割掉  没有就正常
      return day.startsWith('0') ? day.substring(1) : day
    }
  },
  data() {
    //   当前日期
    const currentDate = new Date()
    // 当前年份
    const currentYear = currentDate.getFullYear()
    // 当前月份
    const currentMonth = currentDate.getMonth() + 1
    const yearList = []
    // 遍历前后5年
    for (let i = 0; i < 12; i++) {
      yearList.push(currentYear - 5 + i)
    }
    return {
      currentDate,
      currentMonth,
      currentYear,
      yearList,
      holiday: ['2021-09-21', '2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05']
    }
  },
  //   监听日期更新 点击上下月跟进显示
  watch: {
    currentDate(newDate) {
      this.currentYear = newDate.getFullYear()
      this.currentMonth = newDate.getMonth() + 1
    }
  },
  methods: {
    //   点击下拉框日期 下面显示相应的
    getcurrentDate() {
      this.currentDate = new Date(this.currentYear + '-' + this.currentMonth)
    },
    // 休息
    isWeek(date, data) {
      // 周末休  && 只显示本月休  type是类型 比如前月 当月 下月
      const isWeekend = (date.getDay() === 6 || date.getDay() === 0)
      const isHoliday = this.holiday.includes(data.day)
      return (isWeekend || isHoliday) && data.type === 'current-month'
    }
  }
}
</script>

<style  scoped>
 ::v-deep .el-calendar-day {
  height:  auto;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
 .electYear{
     margin-right: 10px;
     width: 150px;
 }
 .electMonth{
     width: 150px;
 }
</style>
